<template>
  <div class="login_page">
    <el-card body-class="box-card">
      <template #header>
        <div class="card_header">
          <span>登入</span>
        </div>
      </template>
      <el-form :model="form" label-width="100px" @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登入</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import  { ref } from 'vue';
import  { ElNotification } from "element-plus";
import router from "../router/index.js";
import {useRouter} from "vue-router";

export default {
  setup() {
    const form = ref({ username: "", password: "" });
    const router = useRouter();

    const handleLogin = () => {

      if (form.value.username === 'admin' || form.value.password === 'admin') {

        localStorage.setItem('isAuthenticated','true');

        ElNotification({
          title: '成功',
          message: '登入成功',
          type: 'success',
        });

        router.push('/page2');

      }else {

        ElNotification({
          title: '错误',
          message: '用户名或密码错误，请重试',
          type: 'error',
        });
      }
    };
    return { form, handleLogin };
  }
};
</script>


<style scoped>
.login_page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 35vh;
  background: #fff000;
}

.box-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.card_header{
  text-align: center;
}
</style>